/**
 * 
 */
package com.jm.web.builder.table;


import java.io.IOException;

import com.jm.utils.PageJsGear;
import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.action.BaseExportActionHelper;
import com.jm.web.builder.BaseBuilder;
import com.jm.web.data.ReturnData;
import com.jm.web.form.Paging;
import com.jm.web.form.table.Table;
import com.jm.web.tags.support.BaseTag;
import com.jm.web.tags.table.TableTag;

/**
 * @author yanjie
 *
 * 2011/9/22 下午1:39:03
 * Table 绘制器
 */
public class TableBuilder extends BaseBuilder{
	TableTag tableTag = null ;
	
	
	@Override
	public void init(BaseTag tag) {
		super.init(tag);
		
		if(tag instanceof TableTag){
			this.tableTag = (TableTag)tag;
		}
	}
	
	/* 
	 * 绘制表头
	 */
	@Override
	public void RenderStart() {
		if(this.tableTag == null)
			return ;
		try {
			if(this.tableTag.getTable() == null)
				return ;
			StringBuilderEx sb = new StringBuilderEx();
			
			if(!this.getBaseTag().getPage().isExport()){
				//表格工具栏
				if(this.getBaseTag().GetExtValue("toolbar",false)){
					this.getOut().write("<div width=\"100%\" >");
					tableTag.getTableToolbar().onRender(this.getOut());
					this.getOut().write("</div>");
				}
				
				//註冊上下左右移動腳本
				if(this.getBaseTag().GetExtValue("jump",false)){
					sb.Reset();
					sb.Append("$P.grid.regKeydownEvent({tid:'%1$s',%2$s});"
							,this.getBaseTag().GetExtValue("id","")
							,this.getBaseTag().GetExtValue("keydownconfig","a:''"));
					this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY, sb.toString());
				}
				
				//表格容器
				if(this.tableTag.GetExtValue("container",false)){
					sb.Reset();
					sb.Append("<div id=\"%1$s_container\" class=\"listing-layout\" style=\"height:%2$s;width:%3$s;%4$s\"><div style=\"+zoom:1;z-index:0;\" >"
//					sb.Append("<div id=\"%1$s_container\" class=\"listing-layout\" style=\"height:%2$s;width:%3$s;%4$s\">"
							,this.tableTag.getTable().getId()
							,this.tableTag.GetExtValue("containerheight","")
							,this.tableTag.GetExtValue("containerwidth","100%")
							,this.tableTag.GetExtValue("containerstyle",""));
					this.getOut().write(sb.toString());
				}
				
				this.tableTag.getTable().onOutItemBegin(this.getOut());
			}else{
				if(this.getBaseTag().getPage().isExportFile() && this.getBaseTag().getPage().isFastExport()){
					//导出xls 
					BaseExportActionHelper export = this.getBaseTag().getPage().getExportAction();
					export.initFile(this.tableTag.getExportTag(),this.tableTag.getExportTag().getExportNode());//初始化xls文件
					export.exportTable(this.tableTag,this.tableTag.getTable());
				}else{
					this.tableTag.getTable().onOutItemBegin(this.getOut());
				}
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*
	 *	绘制表尾
	 */
	@Override
	public void RenderEnd() {
		if(this.tableTag == null)
			return ;
		
		if(this.getBaseTag().getPage().isExport()) return ;
		
		if(this.tableTag.getTable() == null)
			return ;
		
		this.tableTag.getTable().onOutItemEnd(this.getOut());
		
		StringBuilderEx sb = new StringBuilderEx();
		if(this.tableTag.GetExtValue("container",false)){
			sb.Append("</div></div>");
//			sb.Append("</div></div>");
			try {
				this.getOut().write(sb.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
//		绘制分页
		if(this.getBaseTag() instanceof TableTag){
			sb.Reset();
			TableTag tableTag = (TableTag)this.getBaseTag();
			Table table = tableTag.getTable();
			ReturnData ret = this.getBaseTag().getPage().getReturnData();
			Paging paging = (Paging)ret.GetParamValue(StringHelper.Format("paging_%1$s",table.getId()));
			if(paging != null){
				paging.init(this.getBaseTag());
				if(this.getBaseTag().GetExtValue("paging",true)){
					sb.Append(paging.OutputHtmlCode());
				}
				
				sb.Append("\r\n<input type=\"hidden\" id=\"%1$s_page\" name=\"%1$s_page\" value=\"%2$s\" />",
						table.getId(),
						this.getBaseTag().getPage().getUtils().getForm().gpsv("_page").replace("-1",""));//分頁 -1不需要显示
				try {
					this.getOut().write(sb.toString());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		//处理表格的JS
		this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_TABS, PageJsGear.getTableJs(this.getBaseTag().getPage(),this.tableTag));
		
		if(Table.TAG_TABLE_STYLE_FIEXEDROW .equalsIgnoreCase(this.tableTag.getTable().getView())){
			sb.Reset();
			
			if(!StringHelper.IsNullOrEmpty(this.tableTag.GetExtValue("customcontainer",""))){
				sb.Append("$P.fixedtable['%1$s']= new $P.grid.fixedtable('%1$s','%2$s');",this.tableTag.getTable().getId(),this.tableTag.GetExtValue("customcontainer",""));
			}else if(this.tableTag.GetExtValue("container",false)){
				sb.Append("$P.fixedtable['%1$s']= new $P.grid.fixedtable('%1$s','%1$s_container');",this.tableTag.getTable().getId());
			}else{
				sb.Append("$P.fixedtable['%1$s']= new $P.grid.fixedtable('%1$s','');",this.tableTag.getTable().getId());
			}
			
			this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY,sb.toString());
		}
	}
}
